/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.TrafficTypeDao;
import com.tesam.gedeon.entity.TrafficType;
import com.tesam.gedeon.service.TrafficTypeService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class TrafficTypeServiceImpl implements TrafficTypeService {

    @Autowired
    TrafficTypeDao trafficTypeDao;
    
    @Override
    public int getCountAll() {
        return trafficTypeDao.getCountAll() ;
    }

    @Override
    public int getCountBySearch(TrafficType trafficType) {
        return trafficTypeDao.getCountBySearch(trafficType);
    }

    @Override
    public List<TrafficType> getListAll() {
            return trafficTypeDao.getListAll();
    }

    @Override
    public List<TrafficType> getListByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
       return trafficTypeDao.getListByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<TrafficType> getListBySearch(int startLimit, int endLimit, String dataToSort, String directionOfOrder, TrafficType trafficType) {
        return trafficTypeDao.getListBySearch(startLimit, endLimit, dataToSort, directionOfOrder, trafficType);
    }

    @Override
    public void delete(TrafficType trafficType) {
        trafficTypeDao.delete(trafficType);
    }

    @Override
    public int save(TrafficType trafficType) {
        return trafficTypeDao.save(trafficType);
    }

    @Override
    public void update(TrafficType trafficType) {
        trafficTypeDao.update(trafficType)  ;
    }

    @Override
    public List<TrafficType> getTrafficById(TrafficType trafficType) {
        return trafficTypeDao.getTrafficById(trafficType);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<TrafficType> listTrafficType, int quantityRecord){
        JSONArray array = null;
        JSONObject json = null;
        String deleteButton , groupButton  , viewButton , updateButton= "" ; 
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listTrafficType.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (TrafficType trafficType : listTrafficType) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+trafficType.getId()+"&module="+trafficType.getModuleId()+"' > ";
                    viewButton+= " <i class='icon-share'></i> View </a>"; 
                    
                    updateButton = "<a href='update?id="+trafficType.getId()+"&module="+trafficType.getModuleId()+"' class='green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>";
                    
                    deleteButton = "<a class='red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+trafficType.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>"; 
                    
                    groupButton = "<div class='btn-group'>";
                    groupButton+= "<a class='btn mini purple' href='#' data-toggle='dropdown'>";
                    groupButton+= "Function&nbsp;&nbsp;";
                    groupButton+= "<i class='icon-arrow-down'></i>";
                    groupButton+= "</a>";
                    groupButton+= "<ul class='dropdown-menu'>";
                    groupButton+="<li>"+viewButton+"</li>";
                    groupButton+="<li>"+updateButton+"</li>";
                    groupButton+="<li>"+deleteButton+"</li>";                      
                    groupButton+= "</ul>";
                    groupButton+= "</div>"; 
                    
                    arrayPut.put(trafficType.getCode());
                    arrayPut.put(trafficType.getNetwork().getDescription());
                    arrayPut.put(trafficType.getProvider().getName());
                    arrayPut.put(trafficType.getProviderCode());
                    arrayPut.put(StringEscapeUtils.escapeHtml4(trafficType.getProviderName())); 
                    arrayPut.put(trafficType.getProviderSubName());
                    
                    arrayPut.put(groupButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", quantityRecord);
            json.put("iTotalDisplayRecords", quantityRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }
    
    
}
